package com.shadt.cloud.service.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shadt.cloud.dao.BranchChannelDao;
import com.shadt.cloud.dao.BranchDao;
import com.shadt.cloud.dao.RecordDao;
import com.shadt.cloud.entity.CloudBranch;
import com.shadt.cloud.entity.CloudBranchChannel;
import com.shadt.cloud.service.RecordApiService;
import com.shadt.cloud.vo.RecordVo;
import com.shadt.core.util.DateUtil;

@Service
public class RecordApiServiceImpl implements RecordApiService{
	Logger log = Logger.getLogger(this.getClass());
	private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("config");
	@Autowired
	RecordDao dao;
	@Autowired
	BranchDao bdao;
	@Autowired
	BranchChannelDao bcdao;

	@SuppressWarnings("rawtypes")
	public List<RecordVo> get(String appname, String key, Integer start, Integer size) throws Exception {
		CloudBranch branch = bdao.get("from CloudBranch where name='"+appname+"' and status=0");
		List<RecordVo> rvo=new ArrayList<RecordVo>();
		if(branch!=null){
			CloudBranchChannel bc = bcdao.get("from CloudBranchChannel where key='"+key+"' and status=0");
			if(bc!=null){
				String ss = "select max(seq) from community_recordcloudrelease";
				BigInteger sc = dao.countBySql(ss);
				Long s = sc.longValue();
				rvo = dao.getList(key,s,start,size);
				log.info(rvo.size()+"==rvo.size()");
				if(rvo.size()<size){
					if(rvo.size()>0){
						start=Integer.parseInt(rvo.get(rvo.size()).getId());
						size-=rvo.size();
					}
					String sql = "SELECT clc.seq rseq,r.* FROM community_record as r , "
							+ "(select rc.* from community_recordcloudrelease as rc where "
							+ "rc.channelKey='"+key+"' and rc.seq>"+start+") as clc WHERE "
							+ "clc.recordseq=r.seq ";
					if(branch.getStartTime()!=null&&!branch.getStartTime().equals("")){System.out.println(4);
						sql+=" and r.createtime>='"+branch.getStartTime()+"'";
					}
					sql +=" order by clc.seq asc limit 0,"+size;
					List rvos = dao.getBySql(sql);
					Iterator rit = rvos.iterator();
					while (rit.hasNext()) {
						Map mm = (Map) rit.next();
						RecordVo reo = new RecordVo();
						String recordid = mm.get("seq").toString();
						String sqls = "select * from community_recorddata a,community_field b where a.fieldseq = b.seq and a.recordseq="+ recordid;
						List ll = dao.getBySql(sqls);
						Iterator lit = ll.iterator();
						while (lit.hasNext()) {
							Map mmm = (Map) lit.next();
							if (mmm.get("fieldKey").equals("Crosshead")) {
								reo.setTitle(mmm.get("content").toString());
							} else if (mmm.get("fieldKey").equals("AnimationImg")) {
								reo.setCoverimg(mmm.get("content").toString());
							} else if (mmm.get("fieldKey").equals("RecordOne")) {
								reo.setImgA(mmm.get("content").toString());
							} else if (mmm.get("fieldKey").equals("RecordTwo")) {
								reo.setImgB(mmm.get("content").toString());
							} else if (mmm.get("fieldKey").equals("RecordThree")) {
								reo.setImgC(mmm.get("content").toString());
							}else if (mmm.get("fieldKey").equals("orderby")) {
								if(!mmm.get("content").equals("")&&mmm.get("content")!=null){
									reo.setSort(Integer.parseInt(mmm.get("content").toString()));
								}else{
									reo.setSort(0);
								}
							}
							reo.setId(mm.get("rseq").toString());
							reo.setUuid(mm.get("uniqueID").toString());
							reo.setUrl(bundle.getString("record.url")+"?uniqueID="+ mm.get("uniqueID").toString()+"&xgyd="+key+"&createTime="+
									DateUtil.dataToStr(DateUtil.strToDate(mm.get("createtime").toString(),"yyyy-MM-dd"), "yyyy-MM-dd"));
						}
						rvo.add(reo);
					}
					log.info(sql);
					log.info("从数据库中查询到"+rvo.size()+"条记录");
					dao.addList(key, rvo);
				}else{
					log.info("redis=="+rvo.size());
				}
			}else{
				log.info(key+"--此栏目无获取数据权限");
			}
		}else{
			log.info(appname+"--该平台不可用！");
		}
		return rvo;
	}

}
